#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

int GetMonthDay(int year, int month)
{
	int date[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
	{
		return 29;
	}
	else
	{
		return date[month - 1];
	}
}
int main()
{
	int m = 0;
	cin >> m;
	while (m--)
	{
		int year = 0, month = 0, day = 0, days = 0;
		cin >> year >> month >> day >> days;
		day += days;
		while (day > GetMonthDay(year, month))
		{
			day -= GetMonthDay(year, month);
			++month;
			if (month > 12)
			{
				++year;
				month = 1;
			}
		}
		printf("%04d-%02d-%02d\n", year, month, day);
	}
	return 0;
}